﻿Imports System.Windows.Media.Animation
Imports System.Windows.Threading

Partial Public Class AlertWindow
    'Private FadeInAnimation As DoubleAnimation
    'Private FadeOutAnimation As DoubleAnimation
    Private ActiveTimer As DispatcherTimer

    Private Shared AlertCount As Integer

    Private LockObject As New Object
    Private IsClosing As Boolean
    Private Program As LiveProgram

    Public Sub New(ByVal program As LiveProgram)
        InitializeComponent()
        Me.Program = program

        Me.Title = program.Community.Name

        Dim workAreaRectangle = System.Windows.SystemParameters.WorkArea
        SyncLock LockObject
            AlertCount += 1
        End SyncLock

        ' TODO 画面の外に出たときの対処
        Me.Left = workAreaRectangle.Right - Me.Width
        Me.Top = workAreaRectangle.Bottom - Me.Height * AlertCount


        ' TODO Animation
        '_fadeInAnimation = New DoubleAnimation()
        '_fadeInAnimation.From = 0
        '_fadeInAnimation.To = 0.9
        '_fadeInAnimation.Duration = New Duration(TimeSpan.Parse("0:0:1.5"))

        '_fadeOutAnimation = New DoubleAnimation()
        '_fadeOutAnimation.To = 0
        '_fadeOutAnimation.Duration = New Duration(TimeSpan.Parse("0:0:1.5"))

        AddHandler Me.Loaded, New RoutedEventHandler(AddressOf AlertWindow_Loaded)

    End Sub

    Private Sub AlertWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)


        ' TODO Binding
        Me.IconImage.Source = New BitmapImage(Program.Community.IconUri)
        'Me.TitleHyperLink.NavigateUri = Program.Uri
        Me.TitleTextBlock.Text = Program.Title
        Me.ContentTextBlock.Text = Program.Content
        Me.StartDateTimeLabel.Content = Program.StartDateTime.ToString("HH:mm")
        Me.RemainingTimeLabel.Content = "(残り" & Program.StartDateTime.AddMinutes(30).Subtract(Now).Minutes.ToString & "分)" ' TODO Magic Num 30
        Me.Topmost = True



        Me.Visibility = Windows.Visibility.Visible

        ' TODO Animation
        'AddHandler _fadeInAnimation.Completed, New EventHandler(AddressOf FadeInCompleted)
        'Me.BeginAnimation(AlertWindow.OpacityProperty, _fadeInAnimation)
        FadeInCompleted(Me, e)
    End Sub

    Private Sub FadeInCompleted(ByVal sender As Object, ByVal e As EventArgs)
        ActiveTimer = New DispatcherTimer()
        ActiveTimer.Interval = TimeSpan.Parse("0:0:10")

        AddHandler ActiveTimer.Tick, AddressOf FadeOut
        ActiveTimer.Start()
    End Sub


    Private Sub FadeOutCompleted(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
    End Sub

    Private Sub FadeOut(ByVal sender As Object, ByVal e As EventArgs)

        IsClosing = True
        ' TODO Animation
        'AddHandler FadeOutAnimation.Completed, AddressOf FadeOutCompleted
        'Me.BeginAnimation(AlertWindow.OpacityProperty, FadeOutAnimation, HandoffBehavior.SnapshotAndReplace)
        Me.Close()

    End Sub


    Private Sub DesktopAlertBase_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        SyncLock LockObject
            AlertCount -= 1
        End SyncLock
    End Sub

    Private Sub AlertWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If ActiveTimer IsNot Nothing Then
            ActiveTimer.Stop()
        End If
    End Sub

    Private Sub AlertWindow_MouseEnter(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseEnter
        If Not IsClosing AndAlso ActiveTimer IsNot Nothing Then
            ActiveTimer.Stop()
        End If
    End Sub

    Private Sub AlertWindow_MouseLeave(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseLeave
        If Not IsClosing AndAlso ActiveTimer IsNot Nothing Then
            ActiveTimer.Start()
        End If
    End Sub

    Private Sub MenuItem_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            Clipboard.SetText(Me.Program.Uri.ToString)
        Catch ex As ArgumentNullException
            ' Ignore
        End Try
    End Sub

    Private Sub OpenUri(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Alerter.OpenUri(Me.Program.Uri)
    End Sub
End Class
